/*
 * Copyright (c) 2009 Enio Perpetuo
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); 
 * you may not use this file except in compliance with the License. 
 * You may obtain a copy of the License at 
 * 
 * 		http://www.apache.org/licenses/LICENSE-2.0
 *  
 * Unless required by applicable law or agreed to in writing, software 
 * distributed under the License is distributed on an "AS IS" BASIS, 
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
 * See the License for the specific language governing permissions and 
 * limitations under the License.
 *  
 */
package co.za.gvi.example.receive;

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.FileReader;
import java.net.HttpURLConnection;
import java.net.URL;

/**
 * This example simulates the sending of messages through the GVI server.
 * The messages post to the URL are received by the IncomeMessageServlet
 * and processed by the IncomeMessagePrinter that implements the 
 * IncomeMessageHandler interface in this example
 * 
 * @see co.za.gvi.servlet.IncomeMessageServlet
 * @see co.za.gvi.handler.IncomeMessageHandler
 * @see co.za.gvi.example.receive.IncomeMessagePrinter
 * 
 * @author Enio Perpetuo
 */
public class ReceiveMessageExample {
	
	private static final String URL = "http://127.0.0.1:8080/grape/sms";

	public static void main(String[] args) {
		try {
			// Creates a connection to post
			HttpURLConnection connection = (HttpURLConnection)(new URL(URL)).openConnection();
			connection.setRequestMethod("POST");
			connection.setUseCaches(false);
			connection.setDoOutput(true);
			
			// Gets the output
			DataOutputStream out = new DataOutputStream(connection.getOutputStream());
			
			// Gets the input. 
			// You may try with error.xml, receipt.xml and reply.xml
			BufferedReader in = new BufferedReader(new FileReader("etc/example/error.xml"));
			String line;
			
			// Writes the file to the url
			while ((line = in.readLine()) != null) {
				out.writeBytes(line);
			}
	        out.flush();
	        out.close();
	        in.close();
	        
	        // Gets the response
	        connection.getInputStream();
			
		}catch (Exception e) {
			e.printStackTrace();
		}
	}
}